#include "U2EGLPBuffer.h"

#include "U2Exception.h"
#include "U2LogManager.h"
#include "U2StringConverter.h"
#include "U2EGLSupport.h"


U2EG_NAMESPACE_USING


EGLPBuffer::EGLPBuffer(EGLSupport* glsupport, PixelComponentType format,
                       size_t width, size_t height)
: U2GLESPBuffer(format, width, height)
{

}

//Changed the constructor to a member function so that the
//native constructor would be called first. This member
//function is then called from the native constructor.
void EGLPBuffer::initEGLPBuffer()
{

//	These are now initialized in the native constructors.
//	mGLSupport = glsupport;
//        mGlDisplay = mGLSupport->getGLDisplay();
    mEglDrawable = 0;
    ::EGLConfig glConfig = 0;

    bool isFloat = false;
    int bits = 0;

    switch (mFormat)
    {
    case PCT_BYTE:
        bits = 8;
        break;
    case PCT_SHORT:
    case PCT_FLOAT16:
        bits = 16;
        break;
    case PCT_FLOAT32:
        bits = 32;
        break;
    default:
        break;
    }

    if (mFormat == PCT_FLOAT16 || mFormat == PCT_FLOAT32)
    {
        U2_EXCEPT(U2Exception::ERR_NOT_IMPLEMENTED,
                    "No support for Floating point PBuffers",
                    "EGLRenderTexture::initEGLPBuffer");
    }

    EGLint minAttribs[] = {
        EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
        EGL_DEPTH_SIZE, 16,
        EGL_NONE
    };

    EGLint maxAttribs[] = {
        EGL_RED_SIZE, bits,
        EGL_GREEN_SIZE, bits,
        EGL_BLUE_SIZE, bits,
        EGL_ALPHA_SIZE, bits,
        EGL_STENCIL_SIZE, INT_MAX,
        EGL_NONE
    };

    EGLint pBufferAttribs[] = {
		// First we specify the width of the surface...
        EGL_WIDTH, mWidth,
		// ...then the height of the surface...
        EGL_HEIGHT, mHeight,
		/* ... then we specifiy the target for the texture
		that will be created when the pbuffer is created...*/
		EGL_TEXTURE_TARGET, EGL_TEXTURE_2D,
		/*..then the format of the texture that will be created
		when the pBuffer is bound to a texture...*/
        EGL_TEXTURE_FORMAT, EGL_TEXTURE_RGB,
		// The final thing is EGL_NONE which signifies the end.
        EGL_NONE
    };


    glConfig = mGLSupport->selectGLConfig(minAttribs, maxAttribs);

    EGL_CHECK_ERROR;
    mEglDrawable = eglCreatePbufferSurface(mGlDisplay, glConfig, pBufferAttribs);
    EGL_CHECK_ERROR;

    if (!glConfig || !mEglDrawable)
    {
        U2_EXCEPT(U2Exception::ERR_RENDERINGAPI_ERROR,
                    "Unable to create Pbuffer",
                    "EGLPBuffer::EGLPBuffer");
    }
    EGLint glConfigID;
    EGLint iWidth, iHeight;

    eglGetConfigAttrib(mGlDisplay, glConfig, EGL_CONFIG_ID, &glConfigID);
    EGL_CHECK_ERROR;
    eglQuerySurface(mGlDisplay, mEglDrawable, EGL_WIDTH, &iWidth);
    EGL_CHECK_ERROR;
    eglQuerySurface(mGlDisplay, mEglDrawable, EGL_HEIGHT, &iHeight);
    EGL_CHECK_ERROR;

    mWidth = iWidth;
    mHeight = iHeight;
    U2LogManager::getSingleton().logMessage(LML_NORMAL, "EGLPBuffer::create used final dimensions " + U2StringConverter::toString(mWidth) + " x " + U2StringConverter::toString(mHeight));
    U2LogManager::getSingleton().logMessage("EGLPBuffer::create used FBConfigID " + U2StringConverter::toString(glConfigID));

}

EGLPBuffer::~EGLPBuffer()
{
	eglDestroySurface(mGlDisplay, mEglDrawable);
    U2LogManager::getSingleton().logMessage(LML_NORMAL, "EGLPBuffer::PBuffer destroyed");
}
